package com.demo.jingxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.jingxi.common.Result;
import com.demo.jingxi.pojo.Role;
import com.demo.jingxi.pojo.Type;
import com.demo.jingxi.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/type")
@Slf4j
public class TypeController {
    @Autowired
    private TypeService typeService;
    @GetMapping("/list")
    public Result<Map<String,Object>> getUserList(@RequestParam(value = "typeName",required = false) String typeName,
                                                  @RequestParam(value = "typeId" ,required = false) String typeId,
                                                  @RequestParam(value = "pageNo") Long pageNo,
                                                  @RequestParam(value = "pageSize") Long pageSize){
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(typeName),Type::getTypeName,typeName);
        wrapper.eq(StringUtils.hasLength(typeId),Type::getTypeId,typeId);


        Page<Type> page = new Page<>(pageNo,pageSize);
        typeService.page(page, wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);

    }
    @PostMapping
    public Result<?> addType(@RequestBody Type type){
        typeService.save(type);
        return Result.success("新增角色成功");
    }
    @PutMapping
    public Result<?> update(@RequestBody Type type){

        typeService.updateById(type);
        return Result.success("修改角色成功");
    }

    @GetMapping("/{id}")
    public Result<Type> getTypeById(@PathVariable("id") Integer id){
        Type type = typeService.getById(id);
        return Result.success(type);
    }

    @DeleteMapping("/{id}")
    public Result<Type> deleteRoleById(@PathVariable("id") Integer id){
        typeService.removeById(id);
        return Result.success("删除角色成功");
    }

}
